﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="InternetPush.cs" company="Jarno Burger">
//   Jarno Burger
// </copyright>
// <summary>
//   Defines the InternetPush type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MyDomotica
{
    using System;
    using System.Globalization;

    using MyDomotica.Model;

    using ThingSpeak;

    /// <summary>
    /// This class pushes the received internet data on-line./
    /// </summary>
    public class InternetPush
    {
        #region Declare
        /// <summary>
        /// The think speak client. A class that talks to the thing-speak servers.
        /// </summary>
        private readonly ThinkSpeak thinkSpeakClient;

        /// <summary>
        /// This tells that this class has a debug event.
        /// </summary>
        public EventHandler<string> EventDebug;

        /// <summary>
        /// The model. This contains all the actual data.
        /// </summary>
        private readonly Model.Model model;

        /// <summary>
        /// The countdown before sending stuff to internet.
        /// </summary>
        private int startCountDown = 3;
        #endregion

        #region Initialize
        /// <summary>
        /// Initializes a new instance of the <see cref="InternetPush"/> class.
        /// </summary>
        /// <param name="model">The model.</param>
        public InternetPush(Model.Model model)
        {
            this.model = model;

            // create a new think-speak-client that will talk to INTERNET
            this.thinkSpeakClient = new ThinkSpeak(Constants.ThingSpeakUpdateKey, Constants.ThingSpeakDownloadKey);
            this.Debug("Think-speak client is made.");
        }
        #endregion

        #region Actions
        /// <summary>
        /// Updates this instance.
        /// </summary>
        public void Update()
        {
            if (this.startCountDown > 0)
            {
                this.Debug("Internet countdown : " + this.startCountDown);
                this.startCountDown--;
                return;
            }

            var fields = new string[8];
            int response;
            fields[0] = this.model.CurrentTemperature.ToString(CultureInfo.InvariantCulture);
            fields[1] = this.model.CurrentHumidity.ToString(CultureInfo.InvariantCulture);
            fields[2] = this.model.CurrentBrightness.ToString(CultureInfo.InvariantCulture);
            fields[3] = this.model.CurrentPreferredTemperature.ToString(CultureInfo.InvariantCulture);
            fields[4] = ToInt(this.model.CurrentHeaterIsOn);
            fields[5] = ToInt(this.model.CurrentMotion);
            this.thinkSpeakClient.UpdateThinkSpeakFeed(fields, out response);

            this.Debug("Uploaded data. Response " + response);
        }
        #endregion

        #region Debug
        /// <summary>
        /// Creates a debug event out of the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        private void Debug(string message)
        {
            if (this.EventDebug != null)
            {
                this.EventDebug(this, message);
            }
            else
            {
                System.Diagnostics.Debug.WriteLine(message);
            }
        }
        #endregion

        /// <summary>
        /// Converts true or false to a string of "1" or  "0".
        /// </summary>
        /// <param name="value">if set to <c>true</c> [value].</param>
        /// <returns>
        /// a string with a "1" or a "0"
        /// </returns>
        private static string ToInt(bool value)
        {
            if (value)
            {
                return 1.ToString(CultureInfo.InvariantCulture);
            }
            else
            {
                return 0.ToString(CultureInfo.InvariantCulture);
            }
        }
    }
}
